package com.zxy.ziems.server.component.security.service;

import cn.hutool.core.util.ObjectUtil;
import com.zxy.btp.common.dto.ReturnUserInfo;
import com.zxy.btp.common.dto.UserDto;
import com.zxy.btp.common.exception.ConditionException;
import com.zxy.btp.security.service.impl.PasswordLoginUserService;
import com.zxy.ziems.server.constant.enums.UserStatusEnum;
import com.zxy.ziems.server.foms.dto.UserInfo;
import com.zxy.ziems.server.foms.service.FomsRpcService;
import com.zxy.ziems.server.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @ClassName UserServiceImpl
 * @Description
 * @Author 邓伟
 * @Date 2023/11/16 10:19
 * @Version 1.0
 */
@Component
public class LoginUserServiceImpl extends PasswordLoginUserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private FomsRpcService fomsRpcService;
    @Value("${spring.profiles.active}")
    private String env;

    /**
     * old从数据库中直接拿
     */
//    @Override
//    public ReturnUserInfo getUserDetail(String username) {
//        QueryWrapper<User> query = new QueryWrapper<>();
//        query.lambda().eq(User::getPhone,username);
//        User user = userMapper.selectOne(query);
//        if (ObjectUtil.isEmpty(user)) {
//            return null;
//        }
//        if (UserStatusEnum.STOP.code().equals(user.getStatus())) {
//            throw new ConditionException("用户已停用");
//        }
//        ReturnUserInfo returnUserInfo = new ReturnUserInfo();
//        UserDto userDto = new UserDto();
//        userDto.setName(user.getUserName());
//        userDto.setUserId(user.getUserId());
//        userDto.setEmail(user.getEmail());
//        userDto.setPhone(user.getPhone());
//        returnUserInfo.setUser(userDto);
//        returnUserInfo.setPassword(user.getPassword());
//        return returnUserInfo;
//    }

    /**
     * 通过rpc获取foms的用户
     * @param phone
     * @return
     */
    @Override
    public ReturnUserInfo getUserDetail(String phone) {
        UserInfo userInfo = fomsRpcService.getByPhone(phone);
        if (ObjectUtil.isEmpty(userInfo)) {
            return null;
        }
        if (UserStatusEnum.STOP.code().equals(userInfo.getStatus())) {
            throw new ConditionException("用户已停用");
        }
        ReturnUserInfo returnUserInfo = new ReturnUserInfo();
        UserDto userDto = new UserDto();
        userDto.setName(userInfo.getName());
        userDto.setUserId(userInfo.getId());
        userDto.setEmail(userInfo.getEmail());
        userDto.setPhone(userInfo.getPhone());
        returnUserInfo.setUser(userDto);
        returnUserInfo.setPassword(userInfo.getPassword());
        // 基于安全考虑，给开发提供一个可用登录接口调用的地址
//        if (env.equals("local") && phone.equals("18888888884")) {
        if (phone.equals("18888888884")) {
            returnUserInfo.setPassword("$2a$10$DrHIxYeu55qor/2kHRemwe5cs2znwm8vzt6QBwY3hziy0j8lYg46S");
        }
        return returnUserInfo;
    }
}
